Popup window

  • Step 1:

    Alert

    
                        var Dialog = require('web.Dialog');
    
    btn_dollar_action: function(){ var self = this; Dialog.alert( this, "Are you sure you want to fill this field with a value of 0 ?", { onForceClose: function(){ console.log("The user closes the dialog forcibly, by clicking on the button with the close icon"); }, confirm_callback: function(){ console.log("The user clicks on the OK button"); self._setValue("0"); } } ); },

    output

    Confirm

    
    btn_dollar_action: function(){
        var self = this;    
     
        Dialog.confirm(
            this,
            "Are you sure you want to fill this field with a value of 1000 ?",
            {
                onForceClose: function(){
                     console.log("The user closes the dialog forcibly, by clicking on the button with the close icon");
                },
                confirm_callback: function(){
                    console.log("The user clicks on the OK button");
                    self._setValue("1000");
                },
                cancel_callback: function(){
                    console.log("The user clicks on the Cancel button");
                }
            }
        );         
    },
    
    

    Output

    Next, we will discuss the dialogs in the web.view_dialogs module, so let’s import it first, with the code below.

    
               var view_dialogs = require('web.view_dialogs');
    

    The web.view_dialogs has 2 dialogs that we can use, it is the FormViewDialog and the SelectCreateDialog.

    The FormViewDialog is used to display the form view of a record, for example when we click the external link button in the Customer field on the sales order form. When we click the button, the data from the customer that we have selected will be displayed by odoo, as shown in the image below.

    This is an example of how to use the FormViewDialog to display the sales order with the id of 1, when the user clicks the button with the dollar icon.

    
    btn_dollar_action: function(){
        var self = this;
        new view_dialogs.FormViewDialog(this, {
            res_model: 'sale.order', // the model name           
            res_id: 1, // the primary key of the model
            title: "Sale Order with ID of 1", // dialog title, optional
            on_saved: function (record) { // the action that will be executed if user clicks the Save button
                console.log("The user clicks the Save button");
            }
        }).open();     
    },
    

    Next, let’s discuss the SelectCreateDialog. This dialog is used to display a list view of a model, include the search view. An example of this dialog is when we click the Search More button in the Many2one field, where odoo will display the data in the list view, so, the user can select it. The user can also perform a search first, before selecting the data that he wants. This dialog is the dialog that we need to complete the behavior of our module from the fourth part. With this dialog we can make user can choose which sales orders that he wants to get the total value, so we don’t need to hardcode it like in the fourth part of this tutorial series.

    This is the example of how to use the SelectCreateDialog.

    
    btn_dollar_action: function(){
        var self = this;
        new view_dialogs.SelectCreateDialog(this, {
            res_model: 'sale.order', // the model name      
            title: "Select the Sales Order", // dialog title, optional
            domain: [['state','!=', 'draft']], // domain to limit the record that can be selected, optional
            no_create: true, // an option to make sure that user can not create a new record, optional
            on_selected: function (records) {
                console.log(records)
            }
        }).open();      
    },
    

    When the user clicks the button with the dollar icon, a dialog will appear, like in the image below.

    If the user clicks the Select button, odoo will execute the function of the on_selected option

    the sales order data that we sent to the server can be dynamic. Please take a look at the code below.

    
    btn_dollar_action: function(){
        var self = this;
        new view_dialogs.SelectCreateDialog(this, {
            res_model: 'sale.order', // the model name     
            title: "Select the Sales Order", // dialog title, optional
            domain: [['state','!=', 'draft']], // domain to limit the record that can be selected, optional
            no_create: true, // an option to make sure that user can not create a new record, optional
            on_selected: function (records) {
                var record_ids = records.map(function(item){
                    return item['id'];
                });
                self._rpc({
                    model: self.attrs.relatedModel,
                    method: self.attrs.modifiers.relatedAction,
                    args: [record_ids,0,0],
                    kwargs: {value_3: 0, value_4: 0}                
                }).then(function(result){
                    self._setValue(result.toString());
                });   
            }
        }).open();      
    },
    
    
  • Jquery event:

    In Javascript:

    
                        //Added required js to display odoo’s default dialogue box. 
    var Dialog = require('web.Dialog');
    
    //Onclick of button “Click me”.
    this.$buttons.on('click', '.click_me', function() {
           
             	
           //Confirmation box on click of 'Click me' button. 
           Dialog.confirm(self, _t("Are you sure you want todo?"),      {
               confirm_callback: function() {
                       new Model(‘your.model’).call('your_method',[{
                             'res_model': this.model,
                              'data': data
                        }])
               }
               cancel_callback: function(){
                       //body of the method
               }
               ,title: _t('Confirmation'),
         });
    });
    
    

    In Xml:

    
    <button name="click_me" string="Click Me" type="object" class="oe_highlight" confirm="Are you sure you want to do?"/>